package gui;

import java.util.ArrayList;
import java.util.HashMap;

import players.PlayerDelegate;
import players.WAVPlayer;

public class AnimalManager implements PlayerDelegate {

	private HashMap<Integer, Animal> animals;
	private int currentID;
	private AnimalManagerState state;
	private WAVPlayer rightPlayer;
	private WAVPlayer wrongPlayer;
	private WAVPlayer whichAnimalPlayer;
	private WAVPlayer animalSoundPlayer;
	private WAVPlayer animalGoesPlayer;
	private WAVPlayer anotherTurnPlayer;

	public AnimalManager(ArrayList<Animal> animals) {
		initAnimals(animals);
		pickNewAnimal();
		this.resetRightPlayer();
		this.resetWrongPlayer();
		this.resetWhichAnimalPlayer();
		this.resetAnimalSoundPlayer();
		this.resetAnimalGoesPlayer();
		this.resetAnotherTurnPlayer();
		this.state = new AnimalManagerStateInitial(this);
	}

	protected void addAnimal(Animal animal) {
		this.getAnimals().put(animal.getID(), animal);
	}

	protected HashMap<Integer, Animal> getAnimals() {
		return this.animals;
	}

	protected int getCurrentID() {
		return currentID;
	}

	protected AnimalManagerState getState() {
		return state;
	}

	private void initAnimals(ArrayList<Animal> animals) {
		this.animals = new HashMap<Integer, Animal>();
		for (Animal animal : animals) {
			this.animals.put(animal.getID(), animal);
		}
	}

	@Override
	public void isFinishedPlaying() {
		this.getState().isFinishedPlaying();
	}

	protected void pickNewAnimal() {
		int size = this.animals.keySet().size();
		System.out.println(size);
		int random = (int) (Math.random() * size);
		this.currentID = random;
		this.currentID = 3;
		System.out.println(this.currentID);
		this.resetAnimalGoesPlayer();
		this.resetAnimalSoundPlayer();
//		this.currentID = 3;
	}

	protected void playRightPlayer() {
		rightPlayer.start();
		this.resetRightPlayer();
	}

	protected void playWrongPlayer() {
		wrongPlayer.start();
		this.resetWrongPlayer();
	}

	protected void playWhichAnimalPlayer() {
		whichAnimalPlayer.start();
		this.resetWhichAnimalPlayer();
	}

	protected void playAnimalSoundPlayer() {
		animalSoundPlayer.start();
		this.resetAnimalSoundPlayer();
	}

	protected void playAnimalGoesPlayer() {
		animalGoesPlayer.start();
		this.resetAnimalGoesPlayer();
	}

	protected void playAnotherTurnPlayer() {
		anotherTurnPlayer.start();
		this.resetAnotherTurnPlayer();
	}

	protected void setState(AnimalManagerState state) {
		this.state = state;
	}

	protected void processNewID(int ID) {
		this.getState().processNewID(ID);
	}

	public void processIDRemoval(int fID) {
		this.getState().processIDRemoval(fID);
	}

	private void resetRightPlayer() {
		this.rightPlayer = new WAVPlayer("sounds/datisjuist.wav", this);
	}

	private void resetWrongPlayer() {
		this.wrongPlayer = new WAVPlayer("sounds/datisnietgoed.wav", this);
	}

	private void resetWhichAnimalPlayer() {
		this.whichAnimalPlayer = new WAVPlayer("sounds/welkdier.wav", this);
	}

	private void resetAnimalSoundPlayer() {
		this.animalSoundPlayer = new WAVPlayer("sounds/" + this.getAnimals().get(this.getCurrentID()).getSoundName() + ".wav", this);
	}

	private void resetAnimalGoesPlayer() {
		this.animalGoesPlayer = new WAVPlayer("sounds/" + this.getAnimals().get(this.getCurrentID()).getGoesName() + ".wav", this);
	}

	private void resetAnotherTurnPlayer() {
		this.anotherTurnPlayer = new WAVPlayer("sounds/alsjenogeenkeerwil.wav", this);
	}
}
